import {CommonUtils} from "./common-utils";

export const numberToCurrency = (number: any) => {
    if(CommonUtils.isNull(number) || CommonUtils.isUndefined(number)){
        return '';
    }else if(isNaN(number)){
        return number;
    }
    number = number+'';
    const num = number.split('.');
    let decimal = Number('0.'+num[1]+"").toString().split('.')[1];
    let str=num[0]+"";
    let yu = str.length % 3; // 余数
    let res = "";  // 保存结果
    for (let i = yu; i < str.length; i = i + 3) {
        res = res + "," + str.substr(i, 3)
    }
    if (yu == 0) {
        res = res.substr(1); // 余数为0，去掉第一个字符“,”
    } else {
        res = str.substr(0, yu) + res;
    }
    const result = res+`${decimal?'.'+decimal:''}`;
    return result.replace('-,','-');
}
